﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using GDIExtension;

namespace Patchworker
{
	
	delegate void RePaintHandler();


	public class MainCanvas : IRenderable
	{
		Panel parent;

		RePaintHandler rePaintHandler;

		Size size;

		Point location=new Point();

		bool showTile = false;

		Pen tileWhitePen = new Pen(Color.White, 3);
		Pen tileBlackPen = new Pen(Color.Black, 1);

		public MainCanvas(Panel parent)
		{

			rePaintHandler += parent.Invalidate;

			parent.ClientSizeChanged += new EventHandler(parent_Resize);

			this.parent = parent;

			ConfigContainer.Instance.refreshHandler += refresh;
		}

		public Point Location
		{
			get
			{
				return location;
			}
		}

		public bool ShowTile
		{
			set
			{
				if (this.showTile != value)
				{
					this.showTile = value;
					rePaintHandler();
				}
			}
		}

		public void refresh()
		{
			rePaintHandler();
			parent_Resize(null, null);
		}

		private void drawTile(Pen pen, Graphics g)
		{
			int numRow = ConfigContainer.Instance.NumHorizontalGrids;
			int numCol = ConfigContainer.Instance.NumVerticalGrids;
			Size tileSize = ConfigContainer.Instance.TileSize;
			Image image = ConfigContainer.Instance.Atlas;

				for (int i = 0; i < numRow+1; i++)
				{
					g.DrawLine(pen, location.X + tileSize.Width*i, location.Y, location.X + tileSize.Width*i, location.Y+image.Height);
				}

				for (int i = 0; i < numCol+1; i++)
				{

					g.DrawLine(pen, location.X, location.Y + tileSize.Height * i, location.X+image.Width, location.Y + tileSize.Height * i);
				}
		}



		void parent_Resize(object sender, EventArgs e)
		{
			if(ConfigContainer.Instance.ImageLoaded)
			{
				int numRow = ConfigContainer.Instance.NumHorizontalGrids;
				int numCol = ConfigContainer.Instance.NumVerticalGrids;
				Size tileSize = ConfigContainer.Instance.TileSize;

				size = new Size(ConfigContainer.Instance.Atlas.Width + 1, ConfigContainer.Instance.Atlas.Height + 1);

				int canvasX;
				int canvasY;

				if (parent.Size.Height > size.Height)
				{
					canvasY = (parent.Size.Height - size.Height)>>1;
				}
				else canvasY = 0;

				if (parent.Size.Width > size.Width)
				{
					canvasX = (parent.Size.Width - size.Width)>>1;
				}
				else canvasX = 0;

				location = new Point(canvasX, canvasY);

			}
		}

		public void render(Graphics g)
		{
			
			if(ConfigContainer.Instance.ImageLoaded)
			{
				g.DrawImage(ConfigContainer.Instance.Atlas, location.X, location.Y, ConfigContainer.Instance.Atlas.Width, ConfigContainer.Instance.Atlas.Height);

				if (showTile)
				{
					drawTile(tileWhitePen, g);
					drawTile(tileBlackPen, g);
				}
			}
		}

		public Bitmap[] TileBitmaps
		{
			get
			{

				int numRow = ConfigContainer.Instance.NumHorizontalGrids;
				int numCol = ConfigContainer.Instance.NumVerticalGrids;
				Size tileSize = ConfigContainer.Instance.TileSize;

				Bitmap[] tiledBitmaps = new Bitmap[numRow * numCol];

				Rectangle cropRect = new Rectangle(0,0, tileSize.Width, tileSize.Height);

				for (int col = 0; col < numCol; col++)
				{
					for (int row = 0; row < numRow; row++)
					{
						cropRect.X = row * tileSize.Width;
						cropRect.Y = col * tileSize.Height;

						tiledBitmaps[numRow * col + row] = ConfigContainer.Instance.Bitmap.Clone(cropRect, ConfigContainer.Instance.Bitmap.PixelFormat);
					}
				}


				return tiledBitmaps;
			}
		}





	}
}
